<?php
class YiiMailer{

public $port;
public $protocol;
public $SMTPSecure;
public $name;
public $to;//кому
public $recipient;//логин получателя
public $from;//email отправителя
public $login; //login отправителя
public $subject;//тема сообщения
public $mail_domain;//домен почтового сервера напр. @mail.ru
public $body;//текст сообщения
public $password;//?
public $charset;
public  $timeout;
public  $debug;
private  $errno;
private  $errstr;

    private function getData($smtp_conn)
    {
        $data="";
        while($str = fread($smtp_conn,1024)){
            $data.=$str;
            if(substr($str,3,1) == " "){break;}
        }
        return $data;
    }
    private function smtpConnection()
    {

            if($this->timeout === null)
            {
                $this->timeout = 10;
            }

            if($this->SMTPSecure == 'ssl')
            {
                $smtp_conn = fsockopen($this->SMTPSecure."://".$this->protocol.".".$this->mail_domain.":".$this->port."",$this->errno, $this->errstr, $this->timeout);
            }
            elseif($this->SMTPSecure === null)
            {
                $smtp_conn = fsockopen($this->protocol.".".$this->mail_domain.":".$this->port."",$this->errno, $this->errstr, $this->timeout);
            }
            else
            {
                throw new CException('Secure protocol not supported.');
            }



        return $smtp_conn;
    }
    private function prepareHeaders()
    {
        if(!$this->charset)
        {
            $this->charset = "utf-8";
        }

        $headers = "Date: ".date("D, j M Y G:i:s")." +0500\r\n";
        $headers.="From: =?".$this->charset."?Q?".str_replace("+","_",str_replace("%","=",urlencode($this->name)))."?= <".$this->login.">\r\n";
        $headers.="X-Mailer: The Bat! (v3.99.3) Professional\r\n";
        $headers.="Reply-To: =?".$this->charset."?Q?".str_replace("+","_",str_replace("%","=",urlencode($this->name)))."?= <".$this->login.">\r\n";
        $headers.="X-Priority: 3 (Normal)\r\n";
        $headers.="Message-ID: <172562218.".date("YmjHis")."@gmail.com>\r\n";
        $headers.="To: =?".$this->charset."?Q?".str_replace("+","_",str_replace("%","=",urlencode($this->recipient)))."?= <".$this->to.">\r\n";
        $headers.="Subject: =?".$this->charset."?Q?".str_replace("+","_",str_replace("%","=",urlencode($this->subject)))."?=\r\n";
        $headers.="MIME-Version: 1.0\r\n";

        if(!empty($this->html)){

            $headers.="Content-Type: text/html; charset=".$this->charset."\r\n";
            $headers.="Content-Transfer-Encoding: 8bit\r\n";
        }
        else
        {

            $headers.="Content-Type: text/plain; charset=".$this->charset."\r\n";
            $headers.="Content-Transfer-Encoding: 8bit\r\n";
        }

        return $headers;
    }

    public function send()
    {
        if($this->html)
        {
           $body = $this->html;
        }
        else
        {
            $body = $this->body;
        }
        $data = '';
        $headers = $this->prepareHeaders();
        $smtp_conn = $this->smtpConnection();
        $size_msg=strlen($headers."\r\n".$this->body);

        fputs($smtp_conn,"EHLO yiimailer\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";

        fputs($smtp_conn,"AUTH LOGIN\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";

        fputs($smtp_conn,base64_encode($this->from)."\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";
        fputs($smtp_conn,base64_encode($this->password)."\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";

        fputs($smtp_conn,"MAIL FROM:<".$this->from."> SIZE=".$size_msg."\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";

        fputs($smtp_conn,"RCPT TO:<".$this->to.">\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";

        fputs($smtp_conn,"DATA\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";

        fputs($smtp_conn,$headers."\r\n".$body."\r\n.\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";

        fputs($smtp_conn,"QUIT\r\n");
        $data .= $this->getData($smtp_conn);
        $data.="<br />";
        if($this->debug){
            echo $data;
        }
    }
}